<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
<!--        <a-button type="primary" @click="handleAdd">新增</a-button>-->
<!--        <a-button type="primary" @click="handleChangeMobile(selectedRow)" :disabled="!hasSelectedRows">换绑手机</a-button>-->
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
    </BasicTable>
    <AppUserDrawer @register="registerAppUserDrawer" @success="handleSuccess"/>
    <UserChangeMobileModal @register="registerUserChangeMobileModal" @success="handleSuccess"/>
    <UserDetailModal @register="registerUserDetailModel"/>
  </div>
</template>
<script lang="ts" setup name="appUser">
import {ActionItem, BasicTable, TableAction} from '@/components/table';
import {useDrawer} from '@/components/drawer';
import {useBaseTable} from '@/hooks/flyme/useBaseTable';
import {columns, searchFormSchema} from './data/data';
import {Api, resetPwd} from './data/api';
import {useModal} from '@/components/modal';
import {useMessage} from '@/hooks/web/useMessage';
import UserChangeMobileModal from './components/UserChangeMobileModal.vue';
import UserDetailModal from './components/UserDetailModal.vue';
import AppUserDrawer from './components/AppUserDrawer.vue';

const {prefixCls, tableContext, handleSuccess, handleDelete, handleBatchDelete, handlePost} = useBaseTable({
  designScope: 'appUser-template',
  tableProps: {
    //表格标题
    title: '用户列表',
    //主键字段
    rowKey: 'userId',
    //列表查询handler
    handlerName: 'appUserInfoListHandler',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      showExportButton: false,
      exportButtonOptions: {
        code: 'ACTION_appUserExport',
      },
      schemas: searchFormSchema,
    },
    //默认排序(排序字段上需设置defaultSortOrder:'descend')
    defSort: {
      order: 'user.createTime',
      sort: 'desc',
    },
    //操作列配置
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      slots: {customRender: 'action'},
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});
const [registerTable, {reload}, {rowSelection, hasSelectedRows, selectedRow}] = tableContext;
const [registerAppUserDrawer, {openDrawer: openUserFormDrawer}] = useDrawer();
const [registerUserChangeMobileModal, {openModal: openUserChangeMobileModal}] = useModal();
const [registerUserDetailModel, {openModal: openUserDetailModal}] = useModal();
const {createConfirm} = useMessage();

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'ACTION_editAppUser',
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        icon: 'ant-design:delete-outlined',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '重置密码',
      popConfirm: {
        title: '确认重置密码为123456789吗?',
        icon: 'ant-design:delete-outlined',
        confirm: resetPwd.bind(null, record.userId),
      },
    }
  ];
}

/**
 * 添加用户
 */
function handleAdd() {
  openUserFormDrawer(true, {
    isUpdate: false,
    showFooter: true,
  });
}

function handleEdit(record: Recordable) {
  openUserFormDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleChangeMobile(record: Recordable) {
  openUserChangeMobileModal(true, {
    record,
    isUpdate: true,
  });
}


function handleResetPassword(record: Recordable) {
  let userId = record.userId;
  createConfirm({
    iconType: 'info',
    title: '重置密码',
    content: '确认重置密码为888888吗？',
    onOk: async () => {
      await resetPwd(userId);
    },
  });
}

function handlerUserDetail(record) {
  openUserDetailModal(true, {
    record,
    isUpdate: false,
  });
}
</script>
